package com.example.task;

import com.example.entity.Order;
import com.example.entity.OrderEvaluation;
import com.example.service.OrderEvaluationService;
import com.example.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class OrderTask {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderEvaluationService orderEvaluationService;

    // 每天午夜执行一次
    @Scheduled(cron = "0 0 0 * * ?")
    public void autoConfirmReceipt() {
        log.info("执行订单确认定时任务");
        List<Order> ordersToConfirm = orderService.getOrdersToAutoConfirm();

        for (Order order : ordersToConfirm) {
            orderService.confirmReceipt(order.getId());
        }
    }

    // 每天午夜执行一次
    @Scheduled(cron = "0 0 0 * * ?")
    public void autoDefaultEvaluation() {
        log.info("执行订单评价定时任务");
        List<OrderEvaluation> evaluationsToDefault = orderEvaluationService.getEvaluationsToAutoDefault();

        for (OrderEvaluation evaluation : evaluationsToDefault) {
            orderEvaluationService.defaultEvaluation(evaluation.getId());
        }
    }
}